/*   Copyright 2012 Julian Aloofi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "ProcessManager.hpp"

Tacticus::ProcessManager *Tacticus::ProcessManager::instance = 0;

Tacticus::ProcessManager &Tacticus::ProcessManager::getInstance()
{
    if (!instance)
        instance = new ProcessManager();
    return *instance;
}

void Tacticus::ProcessManager::destroy()
{
    if (instance)
        delete instance;
    instance = 0;
}

Tacticus::ProcessManager::~ProcessManager()
{
    abortAllProcesses(true);
    clearAllProcesses();
}

std::pair<int, int> Tacticus::ProcessManager::updateProcesses(
    unsigned long deltaMs)
{
    int successCount = 0;
    int failCount = 0;

    std::list<boost::shared_ptr<Process> >::iterator it = processList.begin();
    while (it != processList.end())
    {
        boost::shared_ptr<Process> currentProcess = (*it);
        
        /*  save the iterator and increment the old one if we need to remove
            the process from the list */
        std::list<boost::shared_ptr<Process> >::iterator currentIt = it;
        it++;

        if (currentProcess->getState() == Process::UNINITIALIZED)
            currentProcess->onInit();
        if (currentProcess->getState() == Process::RUNNING)
            currentProcess->onUpdate(deltaMs);
        
        if (currentProcess->isDead())
        {
            switch (currentProcess->getState())
            {
            case Process::SUCCEEDED:
            {
                currentProcess->onSuccess();
                boost::shared_ptr<Process> child =
                    currentProcess->removeChild();
                if (child)
                    attachProcess(child);
                else
                    successCount++;
            break;
            }
            case Process::FAILED:
            {
                currentProcess->onFail();
                failCount++;
            break;
            }
            case Process::ABORTED:
            {
                currentProcess->onAbort();
                failCount++;
            break;
            }
            default:
            {
                break;
            }
            }
            processList.erase(currentIt);
        }
    }
    return std::pair<int, int>(successCount, failCount);
}

boost::weak_ptr<Tacticus::Process> Tacticus::ProcessManager::attachProcess(
    boost::shared_ptr<Tacticus::Process> process)
{
    processList.push_back(process);
    return boost::weak_ptr<Process>(process);
}

void Tacticus::ProcessManager::abortAllProcesses(bool immediate)
{
    std::list<boost::shared_ptr<Process> >::iterator it = processList.begin();
    while (it != processList.end())
    {
        std::list<boost::shared_ptr<Process> >::iterator tempIt = it;
        it++;

        boost::shared_ptr<Process> process = *tempIt;
        if (process->isAlive())
        {
            process->setState(Process::ABORTED);
            if (immediate)
            {
                process->onAbort();
                processList.erase(tempIt);
            }
        }
    }
}

unsigned int Tacticus::ProcessManager::getProcessCount() const
{
    return static_cast<unsigned int>(processList.size());
}

void Tacticus::ProcessManager::clearAllProcesses()
{
    processList.clear();
}
